const app = getApp()
import { getPprojectIslandDetail, getIslandSpaceList, getBusinessTypeList, postIslandAddOrUpdateSpace, postIslandDeleteSpace } from '../../../../api/index'

Page({
  data: {
    projectId: '',
    safeBottom: 0,
    list: [],
    basicInfo: {},
    showInfoModal: false,
    businessList: [],
    business: [],
    activeBusiness: '',
    businessTitle: ''
  },
  changeBusiness ({ detail: { value } }) {
    this.setData({ activeBusiness: this.data.business[value].id, businessTitle: this.data.business[value].title })
    this.getList()
  },
  async deleteSpace ({ currentTarget: { dataset: { item } } }) {
    try {
      const { confirm } = await wx.$pro.showModal({ title: '提示', content: '确定要删除该空间吗？' })
      if (confirm) {
        await postIslandDeleteSpace({ spaceId: item.spaceId })
        wx.showToast({ title: '删除成功！', icon: 'none' })
        this.getList()
      }
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  editSpace ({ currentTarget: { dataset: { item } } }) {
    this.data.basicInfo = {
      title: '编辑空间',
      value: item.spaceName,
      key: 'spaceName',
      spaceId: item.spaceId,
      placeholder: '请输入空间名称'
    }
    this.setData({ showInfoModal: true, basicInfo: this.data.basicInfo })
  },
  addSpace () {
    this.data.basicInfo = {
      title: '添加空间',
      value: '',
      key: 'spaceName',
      placeholder: '请输入空间名称'
    }
    this.setData({ showInfoModal: true, basicInfo: this.data.basicInfo })
  },
  closeInfoPop () {
    this.setData({ showInfoModal: false })
  },
  async changeInfo ({ detail }) {
    try {
      let query = {
        projectId: this.data.projectId,
        businessType: this.data.activeBusiness,
        businessTypeText: this.data.businessTitle
      }
      query[detail.key] = detail.value
      if (detail.spaceId) query.spaceId = detail.spaceId
      await postIslandAddOrUpdateSpace({ ...query })
      // if (detail.spaceId) {
      //   query.spaceId = detail.spaceId
      //   await updateProjectSpace({ ...query })
      // } else {
      //   await addProjectSpace({ ...query })
      // }
      this.closeInfoPop()
      wx.showToast({ title: query.spaceId ? '编辑成功！' : '添加成功！', icon: 'none' })
      this.getList()
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async getDetail () {
    try {
      wx.showLoading({ title: '加载中...', mask: true })
      const { ResultSet } = await getPprojectIslandDetail({ project_id: this.data.projectId })
      // ResultSet.userData.created_at = formatTimeTwo(ResultSet.userData.created_at, 'Y-M-D')
      // if (ResultSet.userData.contact) ResultSet.userData.contact = JSON.parse(ResultSet.userData.contact)
      this.setData({ detail: ResultSet.userData })
      this.getBusiness()
    } catch (err) {
      wx.hideLoading()
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  setBusiness () {
    let business = this.data.detail.business_type.split(',')
    let newBusiness = []
    this.data.businessList.forEach(item => {
      business.forEach(row => {
        if (item.id === parseInt(row)) newBusiness.push(item)
      })
    })
    this.setData({
      business: newBusiness,
      activeBusiness: newBusiness[0].id,
      businessTitle: newBusiness[0].title
    })
  },
  async getBusiness () {
    try {
      const { ResultSet } = await getBusinessTypeList()
      this.setData({ businessList: ResultSet.userData.businessList.value })
      await this.setBusiness()
      this.getList()
    } catch (err) {
      wx.hideLoading()
      console.log(err)
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async getList () {
    try {
      let query = {
        projectId: this.data.projectId,
        type: 1
      }
      if (this.data.activeBusiness) query.businessType = this.data.activeBusiness
      if (this.data.businessTitle) query.businessTypeText = this.data.businessTitle
      const { ResultSet } = await getIslandSpaceList({ ...query })
      this.setData({ list: ResultSet.userData.result })
      wx.hideLoading()
    } catch (err) {
      wx.hideLoading()
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  onLoad ({ id }) {
    const { screenHeight, safeArea: { bottom } } = wx.$pro.getSystemInfoSync()
    this.setData({ safeBottom: screenHeight - bottom, projectId: id })
    this.getDetail()
  }
})